﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MisClient
{
    public partial class FrmRacf : BaseForm
    {
        private IBLL.ISupply supplyBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteSupply();

        private IBLL.IDepart departBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteDepart();

        private IBLL.IShop shopBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteShop();

        private IBLL.IGoods goodsBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteGoods();

        private IBLL.ISys0 sys0BLL = MisCLL.DAOFactory.CreateInstance().GetRemoteSys0();

        private IBLL.IRacf racfBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteRacf();

        public FrmRacf()
        {
            InitializeComponent();
        }

        private void FrmRacf_Load(object sender, EventArgs e)
        {
            //所属部门初始化
            this.cBoxDep.ValueMember = "DEP_ID";
            this.cBoxDep.DisplayMember = "DEPART";
            this.cBoxDep.DataSource = departBLL.GetAll();
            this.cBoxDep.Text = "";

            //仓库初始化
            this.cBoxShop.ValueMember = "SHOP_ID";
            this.cBoxShop.DisplayMember = "SHOP";
            this.cBoxShop.DataSource = shopBLL.GetAll();
            this.cBoxShop.Text = "";

            //供应商初始化
            this.cBoxSupp.ValueMember = "SUPP_ID";
            this.cBoxSupp.DisplayMember = "SUPPLY";
            this.cBoxSupp.DataSource = supplyBLL.GetAll();
            this.cBoxSupp.Text = "";

            this.txtShopCode.Text = this.cBoxShop.SelectedValue.ToString();
        }

        protected void CalculateTotal()
        {
            if (this.dataGridView1.Rows.Count > 0)
            {
                DataGridViewRow dr = null;
                decimal TotalNum = 0;
                decimal TotalQuantity = 0;
                decimal TotalSumJe = 0;
                for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                {
                    dr = this.dataGridView1.Rows[i];
                    TotalNum += decimal.Parse(dr.Cells["SL_P"].Value.ToString());
                    TotalQuantity += decimal.Parse(dr.Cells["SL"].Value.ToString());
                    TotalSumJe += decimal.Parse(dr.Cells["JE"].Value.ToString());
                }
                this.txtTotalNum.Text = decimal.ToInt32(TotalNum).ToString();
                this.txtTotalQuantity.Text = decimal.ToInt32(TotalQuantity).ToString();
                this.txtSumJe.Text = String.Format("{0:N2}", TotalSumJe);
                this.txtNoSumJe.Text = String.Format("{0:N4}", TotalSumJe / decimal.Parse("1." + this.txtTax.Text.Trim()));
            }
            else
            {
                this.txtTotalNum.Clear();
                this.txtTotalQuantity.Clear();
                this.txtSumJe.Clear();
                this.txtNoSumJe.Clear();
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.txtOldNo.Text = "00";
            this.txtBeiY.Clear();
            this.txtOper.Text = accountUser.UserCode;
            this.txtDate.Text = DateTime.Now.ToShortDateString();
            this.dtpDateJH.Text = DateTime.Now.ToShortDateString();
            this.txtOperYS.Clear();
            this.txtDepCode.Text = accountUser.DeptCode;
            this.cBoxDep.SelectedValue = accountUser.DeptCode;
            this.txtOperJH.Text = accountUser.UserCode;
            this.txtShopCode.Text = "";
            this.cBoxSupp.Text = "";
            this.txtDateFK.Text = DateTime.Now.ToShortDateString();
            this.txtOperKC.Text = accountUser.Oper;
            this.txtBei.Clear();
            this.dataGridView1.Rows.Clear();

            this.txtID.Clear();
            this.txtDM.Clear();
            this.txtGoods.Clear();
            this.txtSL_P.Clear();
            this.txtUnit_P.Clear();
            this.txtPack.Clear();
            this.txtTax.Clear();
            this.txtLastPrice.Clear();
            this.txtSL.Clear();
            this.txtUnit.Clear();
            this.txtPrice.Clear();
            this.txtJe.Clear();
            this.txtCas.Clear();
            this.txtGoodsBei.Clear();
            this.txtGoodsBei1.Clear();
            this.txtSLLast.Clear();
            this.txtJeLast.Clear();
            this.txtTotalNum.Clear();
            this.txtTotalQuantity.Clear();
            this.txtSumJe.Clear();
            this.txtNoSumJe.Clear();

            this.txtOldNo.Enabled = true;
            this.txtBeiY.Enabled = true;
            this.cBoxDep.Enabled = true;
            this.cBoxSupp.Enabled = true;
            this.cBoxShop.Enabled = true;
            this.dtpDateJH.Enabled = true;
            this.txtOperYS.Enabled = true;
            this.txtOperJH.Enabled = true;
            this.txtShopCode.Enabled = true;
            this.txtDepCode.Enabled = true;
            this.txtSupp.Enabled = true;
            this.txtOperKC.Enabled = true;
            this.txtBei.Enabled = true;

            this.btnAdd.Enabled = false;
            this.btnAddRecord.Enabled = false;
            this.btnDelRecord.Enabled = false;
            this.btnPrint.Enabled = false;
            this.btnRef.Enabled = true;
            this.btnQuit.Enabled = false;
            this.btnSave.Enabled = false;
            this.btnCan.Enabled = true;
            this.txtOldNo.Focus();
        }

        private void btnCan_Click(object sender, EventArgs e)
        {
            this.txtOldNo.Enabled = false;
            this.txtBeiY.Enabled = false;
            this.cBoxDep.Enabled = false;
            this.cBoxSupp.Enabled = false;
            this.cBoxShop.Enabled = false;
            this.dtpDateJH.Enabled = false;
            this.txtOperYS.Enabled = false;
            this.txtOperJH.Enabled = false;
            this.txtShopCode.Enabled = false;
            this.txtDepCode.Enabled = false;
            this.txtSupp.Enabled = false;
            this.txtOperKC.Enabled = false;
            this.txtBei.Enabled = false;

            this.txtID.Enabled = false;
            this.txtSL_P.Enabled = false;
            this.txtUnit_P.Enabled = false;
            this.txtPack.Enabled = false;
            this.txtTax.Enabled = false;
            this.txtLastPrice.Enabled = false;
            this.txtSL.Enabled = false;
            this.txtUnit.Enabled = false;
            this.txtPrice.Enabled = false;
            this.txtJe.Enabled = false;
            this.txtCas.Enabled = false;
            this.txtGoodsBei.Enabled = false;
            this.txtGoodsBei1.Enabled = false;

            this.btnAdd.Enabled = true;
            this.btnAddRecord.Enabled = false;
            this.btnDelRecord.Enabled = false;
            this.btnPrint.Enabled = true;
            this.btnRef.Enabled = true;
            this.btnQuit.Enabled = true;
            this.btnSave.Enabled = false;
            this.btnCan.Enabled = false;
        }

        private void txtOldNo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtDepCode.Focus();
            }
        }


        private void dtpDateJH_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtBeiY.Focus();
            }
        }

        private void txtBeiY_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtOperJH.Focus();
            }
        }

        private void txtOperJH_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtOperYS.Focus();
            }
        }

        private void txtOperYS_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtDepCode.Focus();
            }
        }

        private void cBoxOperWJ_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtOperKC.Focus();
            }
        }

        private void txtOperKC_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtBei.Focus();
            }
        }

        private void txtBei_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (string.IsNullOrEmpty(this.txtShopCode.Text))
                {
                    MessageBox.Show("请选择退货仓库！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtShopCode.Focus();
                    return;
                }
                this.txtOldNo.Enabled = false;
                this.txtBeiY.Enabled = false;
                this.txtDepCode.Enabled = false;
                this.txtSupp.Enabled = false;
                this.cBoxShop.Enabled = false;
                this.cBoxDep.Enabled = false;
                this.cBoxSupp.Enabled = false;
                this.dtpDateJH.Enabled = false;
                this.txtOperYS.Enabled = false;
                this.txtOperJH.Enabled = false;
                this.txtShopCode.Enabled = false;
                this.txtOperKC.Enabled = false;
                this.txtBei.Enabled = false;

                this.txtID.Enabled = true;
                this.txtSL_P.Enabled = true;
                this.txtUnit_P.Enabled = true;
                this.txtPack.Enabled = true;
                this.txtTax.Enabled = true;
                this.txtLastPrice.Enabled = true;
                this.txtSL.Enabled = true;
                this.txtUnit.Enabled = true;
                this.txtPrice.Enabled = true;
                this.txtJe.Enabled = true;
                this.txtCas.Enabled = true;
                this.txtGoodsBei.Enabled = true;
                this.txtGoodsBei1.Enabled = true;

                this.btnAdd.Enabled = false;
                this.btnAddRecord.Enabled = true;
                this.btnDelRecord.Enabled = true;
                this.btnPrint.Enabled = false;
                this.btnRef.Enabled = true;
                this.btnQuit.Enabled = false;
                this.btnSave.Enabled = true;
                this.btnCan.Enabled = true;
                this.txtID.Focus();
            }
        }

        private void txtID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (string.IsNullOrEmpty(this.txtID.Text))
                {
                    MessageBox.Show("请输入合法的商品条码！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    MisEntity.Goods entity = goodsBLL.EndsWithID(this.txtID.Text.Trim());
                    if (entity == null)
                    {
                        MessageBox.Show("无此商品，请先设置！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        this.txtID.Text = entity.ID.Trim();
                        this.txtDM.Text = entity.DM.Trim();
                        this.txtGoods.Text = entity.GOODS.Trim();
                        this.txtUnit_P.Text = entity.UNIT_P.Trim();
                        this.txtPack.Text = entity.PACK.ToString();
                        this.txtTax.Text = entity.Tax.ToString();
                        this.txtUnit.Text = entity.UNIT.Trim();
                        this.txtPrice.Text = String.Format("{0:N4}", entity.Je_last / entity.SL_last);
                        this.txtLastPrice.Text = this.txtPrice.Text;
                        this.txtSLLast.Text = String.Format("{0:N1}", entity.SL_last);
                        this.txtJeLast.Text = String.Format("{0:N1}", entity.Je_last);
                        this.txtSL_P.Focus();
                    }

                }
            }
        }

        private void txtSL_P_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtSL_P.Text))
                {
                    decimal n;
                    if (decimal.TryParse(this.txtSL_P.Text, out n) && decimal.TryParse(this.txtPack.Text, out n))
                    {
                        this.txtSL.Text = String.Format("{0:N2}", decimal.Parse(this.txtPack.Text.Trim()) * decimal.Parse(this.txtSL_P.Text.Trim()));
                        this.txtSL.Focus();
                    }
                }

            }
        }

        private void txtSL_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtPrice.Text) && !string.IsNullOrEmpty(this.txtSL.Text))
                {
                    decimal n;
                    if (decimal.TryParse(this.txtSL.Text, out n) && decimal.TryParse(this.txtPrice.Text, out n))
                    {
                        this.txtJe.Text = String.Format("{0:N2}", decimal.Parse(this.txtPrice.Text.Trim()) * decimal.Parse(this.txtSL.Text.Trim()));
                        this.txtPrice.Focus();
                    }
                }
            }
        }

        private void txtPrice_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtPrice.Text) && !string.IsNullOrEmpty(this.txtSL.Text))
                {
                    decimal n;
                    if (decimal.TryParse(this.txtSL.Text, out n) && decimal.TryParse(this.txtPrice.Text, out n))
                    {
                        this.txtJe.Text = String.Format("{0:N2}", decimal.Parse(this.txtPrice.Text.Trim()) * decimal.Parse(this.txtSL.Text.Trim()));
                        this.txtJe.Focus();
                    }
                }
            }
        }

        private void txtJe_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtCas.Focus();
            }
        }

        private void txtCas_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btnAddRecord.Focus();
            }
        }

        private void btnAddRecord_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtID.Text))
            {
                MessageBox.Show("请输入合法的商品条码！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (string.IsNullOrEmpty(this.txtSL.Text))
            {
                MessageBox.Show("请输入数量不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            String[] DataRow = new String[10];
            DataRow[0] = this.txtID.Text.Trim();
            DataRow[1] = this.txtDM.Text.Trim();
            DataRow[2] = this.txtGoods.Text.Trim();
            DataRow[3] = this.txtUnit.Text.Trim();
            if (!string.IsNullOrEmpty(this.txtSL.Text.Trim()))
            {
                DataRow[4] = this.txtSL.Text.Trim();
            }
            else
            {
                DataRow[4] = "0";
            }
            DataRow[5] = this.txtPrice.Text.Trim();
            DataRow[6] = this.txtJe.Text.Trim();
            if (!string.IsNullOrEmpty(this.txtSL_P.Text.Trim()))
            {
                DataRow[7] = this.txtSL_P.Text.Trim();
            }
            else
            {
                DataRow[7] = "0";
            }
            DataRow[8] = this.txtCas.Text.Trim();
            DataRow[9] = this.txtTax.Text.Trim();
            this.dataGridView1.Rows.Add(DataRow);

            CalculateTotal();

            this.txtID.Clear();
            this.txtDM.Clear();
            this.txtGoods.Clear();
            this.txtSL_P.Clear();
            this.txtSL.Clear();
            this.txtPrice.Clear();
            this.txtJe.Clear();
            this.txtCas.Clear();
            this.txtGoodsBei.Clear();
            this.txtID.Focus();
        }

        private void btnDelRecord_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.CurrentRow != null)
            {
                this.dataGridView1.Rows.Remove(this.dataGridView1.CurrentRow);
                CalculateTotal();
            }
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.Rows.Count > 0)
            {
                string RacCode = sys0BLL.GetNoRac();
                MisEntity.Rac RacEntity = new MisEntity.Rac();
                RacEntity.No = RacCode;
                RacEntity.old_No = this.txtOldNo.Text.Trim();
                RacEntity.date_jh = this.dtpDateJH.Value;
                RacEntity.Dep_id = this.txtDepCode.Text.Trim();
                RacEntity.Supp_id = this.txtSupp.Text.Trim();
                RacEntity.oper_jh = this.txtOperJH.Text.Trim();
                RacEntity.oper_ys = this.txtOperYS.Text.Trim();
                RacEntity.oper_wj = this.txtShopCode.Text.Trim();
                RacEntity.oper_kc = this.txtOperKC.Text.Trim();
                RacEntity.Bei = this.txtBei.Text.Trim();
                RacEntity.oper = this.txtOper.Text.Trim();
                RacEntity.date = DateTime.Parse(this.txtDate.Text.Trim());
                RacEntity.date_fk = DateTime.Parse(this.txtDateFK.Text.Trim());
                RacEntity.sumje = decimal.Parse(this.txtSumJe.Text.Trim());

                IList<MisEntity.Racf> list = new List<MisEntity.Racf>(this.dataGridView1.Rows.Count);
                MisEntity.Racf RItem = null;
                DataGridViewRow dr = null;

                for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                {
                    dr = this.dataGridView1.Rows[i];
                    RItem = new MisEntity.Racf();
                    RItem.No = RacCode.Trim();
                    RItem.ID = dr.Cells["ID"].Value.ToString().Trim();
                    RItem.DM = dr.Cells["DM"].Value.ToString().Trim();
                    RItem.Dep_id = this.txtDepCode.Text.Trim();
                    RItem.Supp_id = this.txtSupp.Text.Trim();
                    RItem.price = decimal.Parse(dr.Cells["Price"].Value.ToString());
                    RItem.TAX = short.Parse(dr.Cells["Tax"].Value.ToString());
                    RItem.SL_P = int.Parse(dr.Cells["SL_P"].Value.ToString());
                    RItem.UNIT = dr.Cells["UNIT"].Value.ToString().Trim();
                    RItem.sl = decimal.Parse(dr.Cells["SL"].Value.ToString());
                    RItem.je = decimal.Parse(dr.Cells["JE"].Value.ToString());
                    if (!string.IsNullOrEmpty(dr.Cells["Cas"].Value.ToString()))
                    {
                        RItem.Cas = decimal.Parse(dr.Cells["Cas"].Value.ToString());
                    }
                    RItem.shop_id = this.txtShopCode.Text.Trim();
                    RItem.NO_BEI = this.txtBei.Text.Trim();
                    list.Add(RItem);
                }
                racfBLL.Insert(RacEntity, list);
                this.txtNo.Text = RacCode;
                this.txtPNo.Text = RacCode;
                MessageBox.Show(string.Format("单据已存盘，编号：{0}，总金额：{1}。是否打印[y/n]？", RacCode, this.txtSumJe.Text.Trim()), "提问窗", MessageBoxButtons.OK, MessageBoxIcon.Question);

                this.txtOldNo.Enabled = false;
                this.txtBeiY.Enabled = false;
                this.cBoxDep.Enabled = false;
                this.cBoxSupp.Enabled = false;
                this.cBoxShop.Enabled = false;
                this.dtpDateJH.Enabled = false;
                this.txtOperYS.Enabled = false;
                this.txtOperJH.Enabled = false;
                this.txtShopCode.Enabled = false;
                this.txtDepCode.Enabled = false;
                this.txtSupp.Enabled = false;
                this.txtOperKC.Enabled = false;
                this.txtBei.Enabled = false;

                this.txtID.Enabled = false;
                this.txtSL_P.Enabled = false;
                this.txtUnit_P.Enabled = false;
                this.txtPack.Enabled = false;
                this.txtTax.Enabled = false;
                this.txtLastPrice.Enabled = false;
                this.txtSL.Enabled = false;
                this.txtUnit.Enabled = false;
                this.txtPrice.Enabled = false;
                this.txtJe.Enabled = false;
                this.txtCas.Enabled = false;
                this.txtGoodsBei.Enabled = false;
                this.txtGoodsBei1.Enabled = false;

                this.btnAdd.Enabled = true;
                this.btnAddRecord.Enabled = false;
                this.btnDelRecord.Enabled = false;
                this.btnPrint.Enabled = true;
                this.btnRef.Enabled = true;
                this.btnQuit.Enabled = true;
                this.btnSave.Enabled = false;
                this.btnCan.Enabled = false;
            }
        }

        private void txtDepCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtDepCode.Text.Trim()))
                {
                    this.cBoxDep.SelectedValue = this.txtDepCode.Text.Trim();
                    if (this.cBoxDep.SelectedValue == null || this.cBoxDep.SelectedValue.ToString().Trim().Length < 1)
                    {
                        MessageBox.Show("退货部门代码应为1位以上,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtDepCode.Focus();
                    }
                    else
                    {
                        this.txtShopCode.Focus();
                    }
                }
                else
                {
                    MessageBox.Show("不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtDepCode.Focus();
                }
            }
        }

        private void txtSupp_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtSupp.Text.Trim()))
                {
                    this.cBoxSupp.SelectedValue = this.txtSupp.Text.Trim().ToUpper();
                    if (this.cBoxSupp.SelectedValue == null)
                    {
                        MessageBox.Show("供应商代码不存在,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtSupp.Focus();
                    }
                    else
                        this.txtOperKC.Focus();
                }
                else
                {
                    MessageBox.Show("不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtSupp.Focus();
                }
            }
        }

        private void txtShopCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtShopCode.Text.Trim()))
                {
                    this.cBoxShop.SelectedValue = this.txtShopCode.Text.Trim().ToUpper();
                    if (this.cBoxShop.SelectedValue == null)
                    {
                        MessageBox.Show("退货仓库代码不存在,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtShopCode.Focus();
                    }
                    else
                        this.txtSupp.Focus();
                }
                else
                {
                    MessageBox.Show("不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtShopCode.Focus();
                }
            }
        }

        private void cBoxDep_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cBoxDep.SelectedValue != null)
                this.txtDepCode.Text = this.cBoxDep.SelectedValue.ToString().Trim();
        }

        private void cBoxShop_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cBoxShop.SelectedValue != null)
                this.txtShopCode.Text = this.cBoxShop.SelectedValue.ToString().Trim();
        }

        private void cBoxSupp_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cBoxSupp.SelectedValue != null)
                this.txtSupp.Text = this.cBoxSupp.SelectedValue.ToString().Trim();
        }
    }
}
